class Garden extends egret.Sprite
{
    static options =
    {
        createBloomSpeed:80,
        bloomCount:50,
        petalCount://花瓣数量范围
        {
            min: 8,
            max: 15
        },
        petalStretch://花瓣大小范围
        {
            min: 6,
            max: 8
        },
        growFactor:
        {
            min: 0.1,
            max: 1
        },
        bloomRadius://花开半径范围
        {
            min:15,
            max: 20
        },
        //density: 10,//密度
        //growSpeed: 1000 / 60,//生长频率
        color://花朵颜色范围
        {
            rmin: 128,
            rmax: 255,
            gmin: 0,
            gmax: 128,
            bmin: 0,
            bmax: 128,
            opacity: 0.1
        },
        tanAngle: 60
    };
    static circle = 2 * Math.PI;
    public blooms: Bloom[] = [];
    public current = 0;
    public b = [];
    public bloomCount;
    public constructor(bloomCount = Garden.options.bloomCount)
    {
        super();
        this.bloomCount = bloomCount;
        this.addEventListener(egret.Event.ENTER_FRAME,this.render,this);
        var startPos = 10;
        var endPos = 30;
        var dt = (endPos - startPos)/this.bloomCount;
        var a = setInterval(() =>
        {
            var h = Garden.getHeartPoint(startPos);
            var e = true;
            for(var f = 0;f < this.b.length;f++)
            {
                var g = this.b[f];
                var j = Math.sqrt(Math.pow(g[0] - h[0],2) + Math.pow(g[1] - h[1],2));
                if(j < Garden.options.bloomRadius.max * 1.3)
                {
                    e = false;
                    break;
                }
            }
            if(e)
            {
                this.b.push(h);
                this.createRandomBloom(h[0],h[1]);
            }
            if(startPos >= endPos)
            {
                clearInterval(a);
                console.log("draw garden complete");
            }
            else
            {
                startPos += dt;
                this.current++;
            }
        },Garden.options.createBloomSpeed);
    }
    public render()
    {
        for(var a = 0;a < this.blooms.length;a++)
        {
            this.blooms[a].draw();
        }
    }
    public addBloom(a: Bloom)
    {
        this.blooms.push(a);
        this.addChild(a);
    }
    public removeBloom(a)
    {
        for(var c = 0;c < this.blooms.length;c++)
        {
            var d = this.blooms[c];
            if(d === a)
            {
                this.blooms.splice(c,1);
                return this;
            }
        }
    }
    public createRandomBloom(x,y)
    {
        var radius = Garden.randomInt(Garden.options.bloomRadius.min,Garden.options.bloomRadius.max);
        var rgba = Garden.randomrgba(Garden.options.color.rmin,
            Garden.options.color.rmax,Garden.options.color.gmin,
            Garden.options.color.gmax,
            Garden.options.color.bmin,
            Garden.options.color.bmax,
            Garden.options.color.opacity);
        var count = Garden.randomInt(Garden.options.petalCount.min,Garden.options.petalCount.max);
        this.createBloom(x,y,radius,rgba,count);
    }
    public createBloom(x,y,radius,rgba,count)
    {
        new Bloom(new Vector(x,y),radius,rgba,count,this);
    }
    public clear()
    {
        this.blooms = [];
        this.graphics.clear();
        //this.ctx.clearRect(0,0,this.element.width,this.element.height);
    }
    static random(b,a)
    {
        return Math.random() * (a - b) + b;
    }
    static randomInt(b,a)
    {
        return Math.floor(Math.random() * (a - b + 1)) + b;
    }
    static degrad(a)
    {
        return Garden.circle / 360 * a;
    }
    static raddeg(a)
    {
        return a / Garden.circle * 360;
    }
    static rgba(red,green,blue,alpha)
    {
        var color = red * 256 * 256 + green * 256 + blue;
        var info = { color: color,alpha: alpha }
        return info;
    }
    static randomrgba(rmin,rmax,gmin,gmax,bmin,bmax,alpha)
    {
        var red = Math.round(Garden.random(rmin,rmax));
        var green = Math.round(Garden.random(gmin,gmax));
        var blue = Math.round(Garden.random(bmin,bmax));
        var e = 5;
        if(Math.abs(red - green) <= e && Math.abs(green - blue) <= e && Math.abs(blue - red) <= e)
        {
            return this.rgba(rmin,gmax,bmin,alpha);
        }
        else
        {
            return this.rgba(red,green,blue,alpha);
        }
    }
    static getHeartPoint(c)
    {
        var b = c / Math.PI;
        var a = 19.5 * (16 * Math.pow(Math.sin(b),3));
        var d = -20 * (13 * Math.cos(b) - 5 * Math.cos(2 * b) - 2 * Math.cos(3 * b) - Math.cos(4 * b));
        var offsetX = egret.MainContext.instance.stage.stageWidth >> 1;
        var offsetY = egret.MainContext.instance.stage.stageHeight >> 1;
        var scale = 0.7;
        return new Array(offsetX + scale * a,offsetY + scale * d);
    }
    public setProgress(percent)
    {
        
    }
}